package com.yuanwushe.blue.calculator;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import android.animation.StateListAnimator;
import android.annotation.SuppressLint;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import org.javia.arity.Symbol;
import org.javia.arity.Symbols;
import org.javia.arity.SyntaxException;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onClickC(View view) {
        switch (view.getId()) {
            case R.id.but_0:
            case R.id.but_1:
            case R.id.but_2:
            case R.id.but_3:
            case R.id.but_4:
            case R.id.but_5:
            case R.id.but_6:
            case R.id.but_7:
            case R.id.but_8:
            case R.id.but_9:
            case R.id.but_add:
            case R.id.but_sub:
            case R.id.but_mul:
            case R.id.but_div:
            case R.id.but_dot:{
                Button button = (Button) view;
                String newText = (String) button.getText();
                TextView formulaArea = (TextView) findViewById(R.id.formula_area);
                String oldText = (String) formulaArea.getText();
                formulaArea.setText(oldText + newText);
                break;
            }
            case R.id.but_c:{
                TextView resultArea = findViewById(R.id.result_area);
                resultArea.setText("");
                TextView formulaArea = findViewById(R.id.formula_area);
                formulaArea.setText("");
                break;
            }
            case R.id.but_del:{
                TextView formulaArea = findViewById(R.id.formula_area);
                String oldText = (String) formulaArea.getText();
                if (oldText.length() > 0) {
                    formulaArea.setText(oldText.substring(0, oldText.length() - 1));
                }
                break;
            }
            case R.id.but_equ:{
                TextView formulaArea = findViewById(R.id.formula_area);
                String text = (String) formulaArea.getText();
                try {
                    Symbols s = new Symbols();
                    double res = s.eval(text);
                    TextView resultArea = findViewById(R.id.result_area);
                    resultArea.setText(String.valueOf(res));
                    Animation fadeIn = AnimationUtils.loadAnimation(MainActivity.this, R.anim.fade_in);
                    resultArea.startAnimation(fadeIn);

                    Animation fadeOut = AnimationUtils.loadAnimation(MainActivity.this, R.anim.fade_out);
                    formulaArea.startAnimation(fadeOut);
                    fadeOut.setAnimationListener(new Animation.AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {

                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                            TextView formulaArea = findViewById(R.id.formula_area);
                            formulaArea.setText("");
                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {

                        }
                    });
                } catch (SyntaxException e) {
                    Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }
            break;
        }
    }
}
